(function() {

	plus = function plus(dname, email) {
		// var cartListDiv = document.getElementById("nav");
		var CartTable = document.getElementById("cart");

		var childRequest = new XMLHttpRequest();
		var url = "PlusServlet?dname=" + dname + "&email=" + email;

		if (email != "" && dname != "") {
			childRequest.onreadystatechange = function() {
				if (childRequest.readyState == 4 && childRequest.status == 200) {
					var result = childRequest.responseText;
					var re = JSON.parse(result);
					var total;
					CartTable.innerHTML = "";
					for (key in re) {
						if (key == "TotalPrice RMB:") {
							total = re[key];
						} else {
							var dishTr = document.createElement("tr");
							var dnameTd = document.createElement("td");
							var numTd = document.createElement("td");
							var numPlusBtn = document.createElement("button");
							var numMinesBtn = document.createElement("button");

							numTd.innerHTML = re[key];
							dnameTd.innerHTML = key;
							numPlusBtn.innerHTML = "+";
							numMinesBtn.innerHTML = "-";
							numPlusBtn.class = "btn";
							numMinesBtn.class = "btn";
							numPlusBtn.onclick = new plusTest(key, email);
							numMinesBtn.onclick = new minesTest(key, email);
							dishTr.appendChild(dnameTd);
							dishTr.appendChild(numMinesBtn);
							dishTr.appendChild(numTd);
							dishTr.appendChild(numPlusBtn);

							CartTable.appendChild(dishTr);
						}
					}
					var totalTr = document.createElement("tr");
					var totalTd = document.createElement("td");
					var priceTd = document.createElement("td");
					totalTr.id = "total";
					totalTd.innerHTML = "TotalPrice RMB:";
					priceTd.innerHTML = total;
					totalTr.setAttribute("total", total);
					totalTr.appendChild(totalTd);
					totalTr.appendChild(priceTd);
					CartTable.appendChild(totalTr);
				}
			}
			childRequest.open("GET", url, true);
			childRequest.send();
		}

	}

	function plusTest(key, email) {
		return function() {
			plus(key, email);
		}

	}

	function minesTest(key, email) {
		return function() {
			mines(key, email);
		}

	}

})();